﻿using System.IO;
using System;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Deployment;
using Microsoft.VisualStudio.SharePoint.Packages;
using System.ComponentModel.Composition;
using System.DirectoryServices;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Xml;

namespace SPVSX.DeploymentExtensions.QuickDeployment
{
    /// <summary>
    /// TODO: COMMENT
    /// 
    /// This code is a WIP and is in need of improved commenting, exception handling, and potentially
    /// factoring.  Please be nice.
    /// 
    /// Though this code is Open Source, in good faith it is requested that you consult the original author(s)
    /// before changing or re-using it for any public endeavour intended to provide similar functionality, or
    /// to fix simple bugs.  In other words, if you want to create SPVSX2, please let us know first.
    /// </summary>
    /// <remarks>@author Matt Smith, http://blog.mattsmith.co.nz</remarks>
    public class RecycleUtilities
    {
        public static void RecycleAllApplicationPools(ISharePointProjectService service)
        {
            string[] names = GetAllApplicationPoolNames(service);
            foreach (string name in names)
            {
                RecycleApplicationPool(name);
            }
        }

        public static bool RecycleApplicationPool(ISharePointProjectService service, string url)
        {
            string name = GetApplicationPoolName(service, url);
            if (String.IsNullOrEmpty(name))
            {
                return false;
            }
            else
            {
                RecycleApplicationPool(name);
                return true;
            }
        }

        public static void RecycleApplicationPool(string name)
        {
            try
            {
                using (DirectoryEntry appPool = new DirectoryEntry(GetApplicationPoolPath(name)))
                {
                    //appPool.Invoke("Recycle", null);
                    appPool.Invoke("Stop", null);
                    appPool.Invoke("Start", null);
                }
            }
            catch (Exception) { }
        }

        private static string GetApplicationPoolPath(string appPoolName)
        {
            string path = "IIS://localhost/w3svc/apppools/" + appPoolName;
            return path;
        }
        
        public static string GetApplicationPoolName(ISharePointProjectService service, string url)
        {
            string name = service.SharePointConnection.ExecuteCommand<string, string>("SPVSX.Commands.GetApplicationPoolName", url);
            return name;
        }

        public static string[] GetAllApplicationPoolNames(ISharePointProjectService service)
        {
            string[] names = service.SharePointConnection.ExecuteCommand<string[]>("SPVSX.Commands.GetAllApplicationPoolNames");
            return names;
        }

        public static void RestartIIS()
        {
            Process process = new Process();
            process.StartInfo.FileName = System.Environment.SystemDirectory + "\\" + "iisreset.exe";
            process.StartInfo.Arguments = "localhost";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.Start();
            process.WaitForExit();
        }
    }
}